package com.xmg.p2p.base.service.impl;

import com.xmg.p2p.base.domain.Account;
import com.xmg.p2p.base.domain.Iplog;
import com.xmg.p2p.base.domain.Logininfo;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.mapper.IplogMapper;
import com.xmg.p2p.base.mapper.LogininfoMapper;
import com.xmg.p2p.base.service.IAccountService;
import com.xmg.p2p.base.service.ILogininfoService;
import com.xmg.p2p.base.service.IUserinfoService;
import com.xmg.p2p.base.util.BidConst;
import com.xmg.p2p.base.util.MD5;
import com.xmg.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class LogininfoServiceImpl implements ILogininfoService {
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private IAccountService accountService;

    @Autowired
    private IUserinfoService userinfoService;

    @Autowired
    private IplogMapper iplogMapper;

    @Override
    public void register(String username, String password) {
        int count = logininfoMapper.getCountByUsername(username);
        if (count <= 0) {
            Logininfo li = new Logininfo();
            li.setUsername(username);
            li.setPassword(MD5.encode(password));
            li.setState(Logininfo.STATE_NORMAL);
            li.setUserType(Logininfo.USER_CLIENT);
            logininfoMapper.insert(li);
            Account account = new Account();
            account.setId(li.getId());
            accountService.add(account);
            Userinfo ui = new Userinfo();
            ui.setId(li.getId());
            userinfoService.add(ui);
        }else {
            throw new RuntimeException("用户名已存在");
        }
    }

    @Override
    public Logininfo login(String username, String password,String ip,int userType) {
        Logininfo current = logininfoMapper.login(username,MD5.encode(password),userType);
        Iplog iplog = new Iplog();
        iplog.setIp(ip);
        iplog.setLoginTime(new Date());
        iplog.setUserName(username);
        iplog.setUserType(userType);
        if(current!=null){
            UserContext.putCurrent(current);
            iplog.setState(Iplog.STATE_SUCCESS);
        }else {
            iplog.setState(Iplog.STATE_FAILED);
        }
        iplogMapper.insert(iplog);
        return current;
    }

    @Override
    public boolean checkUsername(String username) {
        return logininfoMapper.getCountByUsername(username)>0;
    }

    @Override
    public void initAdmin() {
        int count = logininfoMapper.countByUserType(Logininfo.USER_MANAGER);
        if(count==0){
            Logininfo admin = new Logininfo();
            admin.setUsername(BidConst.DEFAULT_ADMIN_NAME);
            admin.setPassword(MD5.encode(BidConst.DEFAULT_ADMIN_PASSWORD));
            admin.setState(Logininfo.STATE_NORMAL);
            admin.setUserType(Logininfo.USER_MANAGER);
            logininfoMapper.insert(admin);
        }
    }

    @Override
    public List<Map<String, Object>> autoComplete(String keyword) {
        return logininfoMapper.autoComplete(keyword);
    }
}
